package com.sbck.voiceclient2;

import android.graphics.Color;
import android.util.Log;

import com.sbck.voiceclient2.myclass.MessageCategory;
import com.sbck.voiceclient2.myclass.MyMessage;
import com.sbck.voiceclient2.myclass.MessageType;
import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;

public class RecvDataThread extends Thread {
    public Socket cSocket;

    byte[] RxData = new byte[1038];
    public RecvDataThread(Socket socket)
    {
        cSocket = socket;
    }
    @Override
    public void run() {
        Log.i("RecvDataThread", "开始接收收据!");


        int DataLong;
        while (true)
        {
            try
            {
                InputStreamReader inputStream =  new InputStreamReader(cSocket.getInputStream(),"GBK");

                char[] buff = new char[1024];
                int len = -1;

                while ((len = inputStream.read(buff)) != -1)
                {
                    Log.i("RecvDataThread","收到数据长度:="+ len );
                    String msg = new String(buff);
                    Log.i("RecvDataThread", msg);
                }
            }
            catch (SocketTimeoutException e)
            {
                Log.i("rqp-RecvDataThread-tcp", "读超时, 超时计次:"+ MyGlobal.TimeoutCount++);
                if(MyGlobal.TimeoutCount > 60)
                {
                    MyMessage myMessage = new MyMessage(MessageCategory.SocketError);
                    myMessage.messageType = MessageType.HeartTimeout;
                    MyGlobal.HeartCount = 0;
                    EventBus.getDefault().post(myMessage);
                }
            }
            catch (SocketException e)
            {
                Log.i("rqp-RecvDataThread-SocketException", e.getMessage());
                break;
            } catch (IOException e) {
                Log.i("rqp-RecvDataThread-IOException", e.getMessage());
                break;
            }
        }
    }

}
